package com.delu.controller;


import com.delu.data.Result;
import com.delu.em.ResultEnum;
import com.delu.entity.TravelDetail;
import com.delu.entity.TravelOrder;
import com.delu.entity.User;

import com.delu.feign.MessageFegin;
import com.delu.mapper.TravelDetailMapper;
import com.delu.mapper.TravelOrderMapper;
import com.delu.mapper.UserMapper;
import com.delu.utils.IdWorker;
import com.delu.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单详细信息 前端控制器
 * </p>
 *
 * @author 6子的爹
 * @since 2021-12-18
 */
@RestController
@RequestMapping("travel-detail")
@Api(value ="订单服务")
public class TravelDetailController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private TravelDetailMapper travelDetailMapper;
    @Autowired
    private TravelOrderMapper travelOrderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageFegin messageFeign;
        //查询个人订单
    @GetMapping("self")
    @ApiOperation(value = "查询个人订单")
    public Result SelectSelfOrderByUserId(HttpServletRequest request){
        //根据用户登录获取到token值. 。o O
        String strId = null;
        try {
            String authToken = request.getHeader("authToken");
            //使用jwt工具类解析token中存放的内容
            Claims claims = jwtUtil.parseJwt(authToken);
            //获取对象id
            strId = claims.getId();
            //获取存入的用户对象
            claims.getSubject();

        }catch (ExpiredJwtException e){

         return Result.response(ResultEnum.ERROR);

        }
        //将jwt封装的用户转成Long
        Long id = Long.valueOf(strId);
        //将用户id存入用户所输入的user对象,调用修改方法,完成通过id修改,达到"实名认证"的目的\
        Map<String,Object> map = new HashMap<>();

        map.put("userid",id);

        List<TravelDetail> travelDetails = travelDetailMapper.selectByMap(map);



        return Result.response(ResultEnum.SUCCESS).setData(travelDetails);
    }

    //生成订单
    @PostMapping("creat")
    @ApiOperation(value = "订单生成")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "订单实体类",required = true,paramType = "body")
    })
    public Result CreatOrder(@RequestBody TravelOrder order,HttpServletRequest request){
        //根据用户登录获取到token值.
        String strId = null;
        try {
            String authToken = request.getHeader("authToken");
            //使用jwt工具类解析token中存放的内容
            Claims claims = jwtUtil.parseJwt(authToken);
            //获取对象id
            strId = claims.getId();
            //获取存入的用户对象
            claims.getSubject();
            //将jwt封装的用户转成Long
            Long id = Long.valueOf(strId);
            //根据前台传的信息,增加订单表
            order.setUserId(id);
            travelOrderMapper.insert(order);
            //将个人订单实体类new 一个对象
            TravelDetail detail = new TravelDetail();
            //利用脱敏类的操作,将订单表中的部分数据增加进个人订单表
            BeanUtils.copyProperties(order,detail);
            //拿取订单表中的订单号,增加进个人订单
            detail.setOrderId(order.getOrderId());
            //增加个人订单表
            detail.setUserId(id);
            travelDetailMapper.insert(detail);
            //增加完订单,获取所属手机号,发送短信
            User user = userMapper.selectById(id);
            //调用短信fenign接口,发送短信
            messageFeign.send(user.getPhone());
        }catch (ExpiredJwtException e){

            return Result.response(ResultEnum.ERROR);

        }


        return Result.response(ResultEnum.SUCCESS);
    }
    //取消订单
    public Result DeleteOrder(@RequestParam("orderId")TravelOrder order,HttpServletRequest request){
            //根据用户生成的订单号修改订单状态
            order.setOrderStatus(2);
            //修改
            travelOrderMapper.updateById(order);
            //根据订单号查询所属用户id,通过id查询user表获取手机号
            User user = userMapper.selectById(order.getUserId());
            //发送短信
            messageFeign.send(user.getPhone());
        return Result.response(ResultEnum.SUCCESS);
    }
}
